﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using Newtonsoft.Json;
using log4net;
using System.Reflection;
using System.Data;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 曲线管理
    /// </summary>
    public class ChartsController : ApiController
    {

        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        [HttpGet]
        [Route("api/Charts/GetDDL")]
        public IHttpActionResult GetDDL(string uRoleId,string DeptID)
        {
            string sqlStr = "";
            if (uRoleId=="4")
            {
                sqlStr = "select UserID,UserName from UserInfo where DeptID=" + DeptID + " and RoleID=5   ";
            }
            if (uRoleId == "3")
            {
                sqlStr = "select DepID,DepName,DepPID from Dep where DepPID=" + DeptID + "";
            }
            if (uRoleId == "2")
            {
                sqlStr = "select DepID,DepName,DepPID from Dep where DepPID=" + DeptID + "";
            }


            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = null;
            try
            {
                #region 暂注释
                ////Contents contents = new Contents();
                //DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourseFiles().GetListView(" a.TCOID='" + TCOID + "' ORDER by a.TCMark asc ");
                ////Contents result = new Contents();
                //if (dsSearch != null)
                //{
                //    if (dsSearch.Tables.Count > 0)
                //    {
                //        if (dsSearch.Tables[0].Rows.Count > 0)
                //        {
                //            DataRow[] dataRowChild = dsSearch.Tables[0].Select(string.Format(@"TCMark={0}", 0));
                //            DataRow[] dataRowChild2 = dsSearch.Tables[0].Select(string.Format(@"TCMark={0}", 1));
                //            #region 1
                //           // result.vList1 = new List<VM_Content>();
                //            //GetResultContentList(dataRowChild, result, 0);
                //           // GetResultContentList(dataRowChild2, result, 1);
                //            #endregion
                //            //返回token信息
                //            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                //            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                //            //resultMsg.Data = result;
                //        }
                //    }
                //}
                #endregion
                ChartsBLL cb = new ChartsBLL();
                dt = cb.GetList(sqlStr);
                resultMsg.Data = dt;

            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);

        }


        [HttpGet]
        [Route("api/Charts/GetUserNumList")]
        public IHttpActionResult GetUserNumList(string ddlId, string startDate, string endDate, string userRoleId, string parm)
        {
            // DateTime st = Convert.ToDateTime(startTime);
            //DateTime et = Convert.ToDateTime(endTime);

            string sqlStr = "";
            if (ddlId == null)
            {
                if (userRoleId == "4") //4代表角色为车队管理人员
                {
                    sqlStr = "SELECT StudyDate,sum(num) as num FROM V_AttendantNum where  convert(char(10),StudyDate,21)>='" + startDate + "' and convert(char(10),StudyDate,21) <='" + endDate + "' and DeptID='"+parm+"'  group by  StudyDate  order by StudyDate ";
                }
                if (userRoleId == "3") //3代表角色为车间管理人员
                {
                    sqlStr = "SELECT StudyDate,sum(num) as num FROM V_AttendantNum where  convert(char(10),StudyDate,21)>='" + startDate + "' and convert(char(10),StudyDate,21) <='" + endDate + "' and DepPID='" + parm + "'  group by  StudyDate  order by StudyDate ";
                }
                if (userRoleId == "2") //3代表角色为机务段管理人员
                {
                    sqlStr = "SELECT StudyDate,sum(num) as num FROM V_AttendantNum where  convert(char(10),StudyDate,21)>='" + startDate + "' and convert(char(10),StudyDate,21) <='" + endDate + "'  group by  StudyDate  order by StudyDate ";
                }

            }
            else
            {
                if (userRoleId == "3")  //3代表角色为车间管理人员
                {
                    sqlStr = "SELECT StudyDate,sum(num) as num  FROM V_AttendantNum  where convert(char(10),StudyDate,21)>='" + startDate + "' and convert(char(10),StudyDate,21) <='" + endDate + "' and DeptID=" + ddlId + " and StudyDate is not null group by  StudyDate  order by StudyDate";
                }
                else if (userRoleId == "2")  //2代表角色为机务段管理人员
                {
                    sqlStr = "SELECT StudyDate,sum(num) as num  FROM V_AttendantNum  where convert(char(10),StudyDate,21)>='" + startDate + "' and convert(char(10),StudyDate,21) <='" + endDate + "' and DepPID=" + ddlId + " and StudyDate is not null group by  StudyDate  order by StudyDate";
                }
                else
                {
                    sqlStr = "SELECT UserID,UserName,num,StudyDate FROM V_AttendantNum where UserID='" + ddlId + "' and convert(char(10),StudyDate,21)>='" + startDate + "' and convert(char(10),StudyDate,21) <='" + endDate + "'";
                }
                
            }
             

            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = null;
            try
            {

                ChartsBLL cb = new ChartsBLL();
                dt = cb.GetList(sqlStr);
                resultMsg.Data = dt;

            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);

        }


        [HttpGet]
        [Route("api/Charts/GetUserInfo")]
        public IHttpActionResult GetUserInfo(string userId)
        {
            // DateTime st = Convert.ToDateTime(startTime);
            //DateTime et = Convert.ToDateTime(endTime);

            //string sqlStr = "";
            //if (deptId == null)
            //{
            //    sqlStr = "SELECT StudyDate,sum(num) as num FROM V_AttendantNum where  convert(char(10),StudyDate,21)>='" + startDate + "' and convert(char(10),StudyDate,21) <='" + endDate + "' group by  StudyDate  order by StudyDate ";
            //}
            //else
            //{
            //    sqlStr = "SELECT UserID,UserName,num,StudyDate FROM V_AttendantNum where UserID='" + deptId + "' and convert(char(10),StudyDate,21)>='" + startDate + "' and convert(char(10),StudyDate,21) <='" + endDate + "'";
            //}
            string whereStr = " UserID='"+userId+"'";

            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = null;
            try
            {

                BLL.UserInfo bll = new BLL.UserInfo();
                dt = bll.GetList(whereStr).Tables[0];
                resultMsg.Data = dt;

            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);

        }

    }



}
